package com.mingmen.supplier.activity;


import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.lzy.imagepicker.ImagePicker;
import com.mingmen.supplier.R;
import com.mingmen.supplier.adapter.CommodityBasicAdapter;
import com.mingmen.supplier.adapter.NewWeiHuListAdapter;
import com.mingmen.supplier.adapter.WeiHuLeftAllAdapter;
import com.mingmen.supplier.base.BaseBean;
import com.mingmen.supplier.defineview.ClearEditText;
import com.mingmen.supplier.defineview.WeihuDialog;
import com.mingmen.supplier.http.callback.StringCallback;
import com.mingmen.supplier.httpconfig.HttpResponse;
import com.mingmen.supplier.immersionbar.ImmersionBar;
import com.mingmen.supplier.model.CommodityBasicModel;
import com.mingmen.supplier.model.WeiHuModel;
import com.mingmen.supplier.popwindow.PopupWindowCompat;
import com.mingmen.supplier.util.GsonUtil;
import com.mingmen.supplier.util.MyStringUtils;
import com.mingmen.supplier.util.SharePreferenceUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class CommodityBasicActivity extends AppCompatActivity implements CommodityBasicAdapter.OnItemClickListener {

    //返回按钮
    @BindView(R.id.text_return)
    TextView textReturn;

    private ImagePicker imagePicker;

    @BindView(R.id.btn_login)
    Button btn_login;

    private int resurtResurant = 100;
    @BindView(R.id.recycle_shopo)
    RecyclerView mRecyclerView;

    CommodityBasicAdapter commodityAdapter;


    LinearLayoutManager linearLayoutManager;

    @BindView(R.id.linear_return)
    LinearLayout linear_return;
    String token;

    String date_Send;

    List<CommodityBasicModel.DataBean> commoditiesBeanList = new ArrayList<>();

    String categoryId;


    @BindView(R.id.layout_nodata)
    LinearLayout layout_nodata;

    String commodityIds_Send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity_basic);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
        linearLayoutManager = new LinearLayoutManager(this);
        token = (String) SharePreferenceUtil.get(this, "token", "");
//        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        commodityIds_Send = getIntent().getStringExtra("commodityIds_Send");

        mRecyclerView.setLayoutManager(linearLayoutManager);
        commodityAdapter = new CommodityBasicAdapter(this, commoditiesBeanList);
        mRecyclerView.setAdapter(commodityAdapter);

        commodityAdapter.setDataList(commoditiesBeanList);
        commodityAdapter.setOnItemClickListener(this);
        weihuDialog = new WeihuDialog(CommodityBasicActivity.this);
        getData(token, commodityIds_Send);

    }


    public void getData(String token, String commodityIds) {
        HttpResponse.getCommodityBatch(token, commodityIds, new StringCallback(CommodityBasicActivity.this) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                Log.d("CommodityBasic", s);
                CommodityBasicModel commodityBasicModel = new CommodityBasicModel();
                commodityBasicModel = GsonUtil.GsonToBean(s, CommodityBasicModel.class);
                commoditiesBeanList.addAll(commodityBasicModel.getData());
                commodityAdapter.setDataList(commoditiesBeanList);

            }
        });

    }

    @OnClick({R.id.text_return, R.id.btn_login, R.id.linear_return})
    public void onViewClicked(View view) {
        switch (view.getId()) {


            case R.id.text_return:
            case R.id.linear_return:
                finish();
                break;
            case R.id.btn_login:
                List<String> sendList=new ArrayList<>();
                JSONObject jsonObject1=new JSONObject();
                JSONObject jsonObject=new JSONObject();
                JSONArray jsonArray=new JSONArray();

                for(int i=0;i<commoditiesBeanList.size();i++){
                    try {
                        jsonObject.put("commodityId",commoditiesBeanList.get(i).getId());
                        jsonObject.put("price",commoditiesBeanList.get(i).getPrice());
                        jsonObject.put("amount",commoditiesBeanList.get(i).getPrice());
//                        sendList.add(jsonObject.toString());
                        jsonArray.put(jsonObject);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                try {
                    jsonObject1.put("sendList",jsonArray);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Intent intent = new Intent(CommodityBasicActivity.this, CommodityShopManagerActivity.class);
                intent.putExtra("jdata",jsonObject1.toString());
                setResult(100, intent);
                finish();
                break;

        }
    }

    WeihuDialog weihuDialog;

    @Override
    public void onItemClickListener(View view, int pos, List<CommodityBasicModel.DataBean> myLiveList) {
        switch (view.getId()) {
            case R.id.text_kucun:
                weihuDialog.setVerifyClickListener("库存", new WeihuDialog.VerifyClickListener() {
                    @Override
                    public void cancel() {
                    }

                    @Override
                    public void send(String str) {
                        myLiveList.get(pos).setAmount(str);
                        commodityAdapter.notifyDataSetChanged();
                    }
                });
                weihuDialog.show();
                break;

            case R.id.text_baojia:
                weihuDialog.setVerifyClickListener("报价", new WeihuDialog.VerifyClickListener() {
                    @Override
                    public void cancel() {
                    }

                    @Override
                    public void send(String str) {
                        myLiveList.get(pos).setPrice(str);
                        commodityAdapter.notifyDataSetChanged();
                    }
                });
                weihuDialog.show();
                break;

            default:

                break;


        }
    }


}




